java如何从类中获取一个变量到主活动?
在我的Android应用程序(Wheather应用程序)中,我有一个主要活动(在屏幕上显示wheater)和一个类(获取手机的当前位置)。 “Position”类获取纬度和经度,我希望将其发送到我的主要活动中以使用它们。为了做到这一点,我尝试使用getter,但这似乎不起作用。以下是两个类的代码:
位置类:(只需注意结尾的getter)
public class Position extends AppCompatActivity implements LocationListener {
private double longitude;
private double latitude;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}
@Override
public void onLocationChanged(Location location) {
longitude=location.getLongitude();
latitude=location.getLatitude();
}
public double getLongitude1() {
return this.longitude;
}
public double getLatitude1() {
return this.latitude;
}
主要活动:(再次注意我试图使用经纬度的最后四行)
public class MainActivity extends AppCompatActivity {
TextView cityField, detailsField, currentTemperatureField, humidity_field, pressure_field, weatherIcon, updatedField;
Typeface weatherFont;
Position position = new Position();
private double latitude1;
private double longitude1;
private String latitude2;
private String longitude2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");
cityField = (TextView)findViewById(R.id.city_field);
updatedField = (TextView)findViewById(R.id.updated_field);
detailsField = (TextView)findViewById(R.id.details_field);
currentTemperatureField = (TextView)findViewById(R.id.current_temperature_field);
humidity_field = (TextView)findViewById(R.id.humidity_field);
pressure_field = (TextView)findViewById(R.id.pressure_field);
weatherIcon = (TextView)findViewById(R.id.weather_icon);
weatherIcon.setTypeface(weatherFont);
Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {
cityField.setText(weather_city);
updatedField.setText(weather_updatedOn);
detailsField.setText(weather_description);
currentTemperatureField.setText(weather_temperature);
humidity_field.setText("Humidity: "+weather_humidity);
pressure_field.setText("Pressure: "+weather_pressure);
weatherIcon.setText(Html.fromHtml(weather_iconText));
}
});
latitude1 = position.getLatitude1();
longitude1 = position.getLongitude1();
latitude2 = String.valueOf(latitude1);
longitude2 = String.valueOf(longitude1);
asyncTask.execute(latitude2, longitude2); // asyncTask.execute("Latitude", "Longitude")
}
为什么我的安卓显示器上总是显示latitude2=0.0和Longtude2=0.0
# 1 楼答案
无法共享私有变量。把它改成
# 2 楼答案
我建议您在代码中添加以下改革
您需要在MainActivity的onCreate()方法内创建Position类的对象。由于onCreate()在运行其他操作之前运行,因此有必要在该方法中定义Position类
将经度和纬度的变量公开,以便在其他类中访问它们
Position类不需要扩展AppCompatActivity。您可以使用构造函数并在那里定义所有内容,而不是使用这个和OnCreate()方法
# 3 楼答案
您实际上不需要从活动中进行
Position
扩展。我可以理解您正在尝试做什么,您只需要从LocationManager
获取位置,然后将结果发送到MainActivity
。如果只在MainActivity
中创建一个LocationManager
实例,并将位置结果传递给MainActivity
中所需的任何内容,就可以了基本上,我认为您不必创建
Position
类。您可以直接获取位置,然后使用它# 4 楼答案
你有两种不同的活动。不是活动和后台服务。只有一个UI线程运行这些活动。因此,当
MainActivity
正在运行时,Position
活动在后台并且没有运行。并且不能使用Position position = new Position();
创建活动的对象# 5 楼答案
为什么你的
Position
类是Activity
?除非将类作为Activity
启动,否则将永远不会在那里调用onCreate
方法。从中删除AppCompatActivity
,并将onCreate
方法移动到一个单独的方法中,例如getLocation
您还希望将
Context
传递给Position
类。为此创建一个构造函数并将其用于系统调用